package euler.p001_050;

import java.util.Arrays;

import euler.MainEuler;
import euler.helper.NaturalHelper;

public class Euler049 extends MainEuler {

    /*
        The arithmetic sequence, 1487, 4817, 8147, in which each
        of the terms increases by 3330, is unusual in two ways:
        (i) each of the three terms are prime, and,
        (ii) each of the 4-digit numbers are permutations of one another.

        There are no arithmetic sequences made up of three
        1-, 2-, or 3-digit primes, exhibiting this property,
        but there is one other 4-digit increasing sequence.

        What 12-digit number do you form by concatenating the three terms in this sequence?
     */
    public String resolve() {
        for (int i1 = 1000; i1 < 10000; i1++) {
            if ((i1 != 1487) && primeHelper.isPrime(i1)) {
                for (int j = 1; 2*j + i1 < 10000; j++) {
                    int i2 = i1 + j;

                    if (primeHelper.isPrime(i2)) {
                        int[] d1 = NaturalHelper.digitos(i1, 10, true);
                        int[] d2 = NaturalHelper.digitos(i2, 10, true);

                        if (Arrays.equals(d1, d2)) {
                            int i3 = i2 + j;
                            if (primeHelper.isPrime(i3)) {
                                int[] d3 = NaturalHelper.digitos(i3, 10, true);

                                if (Arrays.equals(d1, d3)) {
                                    return String.valueOf(i1) +
                                    String.valueOf(i2) + String.valueOf(i3);
                                }
                            }
                        }
                    }
                }
            }
        }

        return null;
    }
}
